package de.tudresden.st.cbse.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.tudresden.st.cbse.calendar.Calendar;
import de.tudresden.st.cbse.calendar.RemoteCalendarManager;
import de.tudresden.st.cbse.calendar.appointment.Appointment;
import de.tudresden.st.cbse.calendar.appointment.AppointmentType;
import de.tudresden.st.cbse.calendar.user.RemoteUserManager;
import de.tudresden.st.cbse.calendar.user.User;
import de.tudresden.st.cbse.util.RemoteLookup;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = -5040712368437320396L;
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher resultView = req.getRequestDispatcher("register.jsp");                                                                                                                          
        resultView.forward(req,resp);
        
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RemoteUserManager statelessRemoteUserManager = null;
        try {
            statelessRemoteUserManager = RemoteLookup.getUserManager();
        } catch (NamingException e) {
            e.printStackTrace();
            return;
        }
        
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        if (email != null && password != null && name != null && statelessRemoteUserManager.register(email, name, password)) {
            HttpSession session = req.getSession(true);
            session.setAttribute("currentSessionUser", email);
            resp.sendRedirect("calendar");
        } else {
            resp.sendRedirect("register.jsp");
        }
    }

}
